import csv
def read_csv(file_name):
    data = []
    with open(file_name, 'r', newline='', encoding='utf-8') as file:
        reader = csv.reader(file)
        header = next(reader)  # 读取第一行作为项目列表

        # 动态构建项目字典
        total_amounts = {header[i]: 0 for i in range(2, len(header))}

        for row in reader:
            data.append(row)
    return data, total_amounts

def query_data(file_name):
    print("请输入你要查询的省份和项目（支持逗号、空格、下划线作为分隔符，不支持中文符号）。例如：'广东省 衣着 医疗保健'")
    user_input = input().split()

    if len(user_input) < 2:
        print("错误：请输入至少一个省份和一个项目。")
        return

    province = user_input[0].strip()
    projects = [project.strip().lower() for project in user_input[1:]]

    # 读取CSV文件并获取项目字典
    data, total_amounts = read_csv(file_name)

    # 确保输入的省份存在于数据中
    provinces = {row[0] for row in data}
    if province not in provinces:
        print(f"错误：输入的省份'{province}'不在数据中。")
        return

    # 确保输入的项目存在于数据中
    invalid_projects = set(projects) - set(total_amounts.keys())
    if invalid_projects:
        print(f"错误：输入的项目{'、'.join(invalid_projects)}不在数据中。")
        return

    # 计算各个项目消费总额
    for row in data:
        if row[0] == province:
            for i, project in enumerate(projects, start=2):
                try:
                    total_amounts[project] += float(row[i])
                except ValueError:
                    print(f"警告：在计算项目'{project}'的消费金额时遇到非数字值，将忽略该值。")

    print(f"\n你输入的省份是：{province}")
    for project in projects:
        total_amount = total_amounts.get(project, 0.0)
        print(f"{project}的消费金额为：{total_amount:.2f}")


if __name__ == "__main__":
    query_data('C:\\Users\\ASUS\\Desktop\\xiaofei.csv')
